summaryrefslogtreecommitdiff
path: root/src-migrate/pages/shop/product/[slug].tsx
blob: fc72a6b097d46c5069aa08dd8fdf0bcdbbb75503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { GetServerSideProps, NextPage } from 'next'
import React, { useEffect } from 'react'
import dynamic from 'next/dynamic'
import cookie from 'cookie'

import { getProductById } from '~/services/product'
import { getIdFromSlug } from '~/libs/slug'
import { IProductDetail } from '~/types/product'

import { Seo } from '~/components/seo'
import { useRouter } from 'next/router'
import { useProductContext } from '@/contexts/ProductContext'

const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout'), { ssr: false })
const ProductDetail = dynamic(() => import('~/modules/product-detail'), { ssr: false })

type PageProps = {
  product: IProductDetail
}

export const getServerSideProps: GetServerSideProps<PageProps> = (async (context) => {
  const { slug } = context.query
  const cookieString = context.req.headers.cookie;
  const cookies = cookieString ? cookie.parse(cookieString) : {};
  const auth = cookies?.auth ? JSON.parse(cookies.auth) : {};
  const tier = auth?.pricelist || ''

  const productId = getIdFromSlug(slug as string)

  const product = await getProductById(productId, tier)

  if (!product) return { notFound: true }

  return {
    props: { product }
  }
})

const SELF_HOST = process.env.NEXT_PUBLIC_SELF_HOST

const ProductDetailPage: NextPage<PageProps> = ({ product }) => {
  const router = useRouter();

  const { setProduct } = useProductContext();

  useEffect(() => {
    if (product) setProduct(product);
  }, [product, setProduct]);

  return (
    <BasicLayout>
      <Seo
        title={`${product.name} - Indoteknik.com`}
        description='Temukan pilihan produk B2B Industri &amp; Alat Teknik untuk Perusahaan, UMKM &amp; Pemerintah dengan lengkap, mudah dan transparan.'
        openGraph={{
          url: SELF_HOST + router.asPath,
          images: [
            {
              url: product?.image,
              width: 800,
              height: 800,
              alt: product?.name,
            },
          ],
          type: 'product',
        }}
        additionalMetaTags={[
          {
            name: 'keywords',
            content: `${product?.name}, Harga ${product?.name}, Beli ${product?.name}, Spesifikasi ${product?.name}`,
          }
        ]}
        canonical={SELF_HOST + router.asPath}
      />

      <div className='md:container pt-4 md:pt-6'>
        <ProductDetail product={product} />
      </div>
    </BasicLayout>
  )
}

export default ProductDetailPage